home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / ndx300.zip / NDX.H < prev    next >
C/C++ Source or Header  |  1992-10-29  |  4KB  |  101 lines

  1. /*  NDX.H   . . .   Header file for index package   */
  2.  
  3. #ifndef NDX_H
  4. #define NDX_H
  5.  
  6. #include    <string.h>
  7. #include    <stdio.h>
  8. #include    <dir.h>
  9.  
  10. typedef unsigned        WORD;
  11. typedef unsigned long   DWORD;
  12. typedef unsigned char   BYTE;
  13.  
  14. const unsigned stackdepth = 10;     // Maximum tree height
  15. const unsigned nodesize = 1024;     // Bytes in each node
  16. const unsigned maxkey = 128;        // Maximum key length
  17. const unsigned maxendkeys = nodesize - sizeof(WORD) - sizeof(DWORD);
  18.                                     // Maximum key data length
  19. enum  types {NODUPS, FIFO, LIFO};   // File access modes
  20.  
  21. typedef int (*CFN)(char *, char *, ...);    // Comparison function types
  22.  
  23. class Key {                 // Key structure
  24. #ifdef TEST
  25. public:
  26. #endif
  27.     friend Node;
  28.     friend Index;
  29.     DWORD   lson;               // Pointer to left son (seek address in file)
  30.     DWORD   offset;             // Record seek address or number
  31.     char    data[maxkey];       // ASCIZ key
  32.     DWORD   insert(DWORD root);
  33.     Key(){}
  34.     Key(char *, DWORD);
  35.     void    push(Node *);
  36.     Node *  pop(Key *&);
  37.     Key     *prev(Node *);       // Returns previous Key in the Node (not the Index)
  38.     Key     *next(){return (Key *)((char *)this + length());}   // The next one
  39.     BYTE    length() {return 2*sizeof(DWORD) + strlen(data) + 1;}
  40.                             // Length of the Key
  41.     Key     *keycpy(Key *s){return (Key *)(memcpy(this, s, s->length()));}
  42. };
  43.  
  44. class Node {
  45.     friend Key;
  46.     friend Node;
  47.     friend Index;
  48.     DWORD       offset;     // Node offset or free node chain
  49.     unsigned    endkeys;    // First unused data byte in keys
  50.     char        keys[maxendkeys];   // The Key structures
  51.     DWORD   *rson(){return (DWORD *)(keys + endkeys);} // Pointer to right son
  52.     split();                                    // Split node in two
  53.     void    shiftup(Key *, unsigned);           // Make room for key
  54.     void    shiftdn(Key *, unsigned, unsigned); // Delete key
  55. };
  56.  
  57. class Frame {              // State stack frame
  58. friend Key;
  59.     friend Node;
  60.     DWORD   node;               // Offset of node in the index file
  61.     WORD    offset;             // Offset of the Key in that node
  62. };
  63.  
  64. class Index {               // THE index structure
  65.     friend Key;
  66.     friend Node;
  67.     DWORD   root;               // File offset of the root node
  68.     DWORD   eof;                // File offset of the end of file
  69.     DWORD   freelist;           // File offset of free node list
  70.     BYTE    minor,          // Version number
  71.             major,
  72.             mode,           // File access mode
  73.             stacktop;          // Current stack top index
  74.     Key     ckey;           // Current key structure
  75.     Frame   stack[stackdepth];  // Current state
  76.     CFN     cmpfn;              // find() comparison function
  77.     char    filename[MAXPATH];  // Index filename
  78.     FILE    *handle;            // Index file handle
  79.     initcache();
  80.     Node    *newnode();
  81.     Node    *getnode(DWORD);
  82.     DWORD   _find(char *, DWORD);
  83.     void    read(DWORD, void *, unsigned);
  84.     void    write(DWORD, void *, unsigned);
  85.   public:
  86.   static    unsigned hdrsize;
  87.     void    print(char *);
  88.     Index(char *, int, CFN = (CFN)strcmp);   // Constructor to create index
  89.     Index(char *, CFN = (CFN)strcmp);        // Constructor to open old one
  90.     ~Index();                       // Destructor, closes index
  91.     DWORD   insert(char *, DWORD);  // Insert new key
  92.     int     remove(char *, DWORD);  // Remove key
  93.     DWORD   find(char *);           // Find key
  94.     DWORD   findkeyrec(char *, DWORD); // Find key, specific record
  95.     DWORD   first(unsigned = 0);    // First key
  96.     DWORD   next();                 // Next key
  97.     DWORD   prev();                 // Previous key
  98. };
  99.  
  100. #endif  NDX_H
  101.